﻿Imports System.Net
Imports System.Web.Http
Imports Models.Security
Imports Services

Namespace Controllers

    Public Class SecurityController
        Inherits ApiController

        ''' <summary>
        ''' Login into MapGeoData app
        ''' </summary>
        ''' <param name="user">login</param>
        ''' <param name="password">password</param>
        ''' <returns>Object session</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function Login(user As String, password As String, Optional companyId As Integer = -1) As LoginResultVO
            Try
                Dim oService As New Services.Security(Request)
                Dim oEntity As LoginResultVO = oService.Login(user, password, companyId)
                Return oEntity
            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try

        End Function

        ''' <summary>
        ''' Get Companies from user
        ''' </summary>
        ''' <param name="user"></param>
        ''' <param name="password"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function GetUserCompanies(user As String, password As String) As Generic.List(Of Models.Users.CompanyVO)
            Try
                Dim oService As New Services.Security(Request)
                Return oService.GetUserCompanies(user, password)
            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try

        End Function

        ''' <summary>
        ''' Logout and close session
        ''' </summary>
        ''' <returns>Object session</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function Logout() As Boolean
            Try
                Dim oService As New Services.Security(Request)
                Return oService.Logout()
            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try

        End Function

        ''' <summary>
        ''' Get all sessions
        ''' </summary>
        ''' <returns>Array of ActiveSiession</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function GetAllSessions() As IEnumerable(Of ActiveSessionVO)

            Try
                Dim oService As New Services.Security(Request)
                Dim oEntity As IEnumerable(Of ActiveSessionVO) = oService.GetAllSessions()
                Return oEntity
            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try
        End Function

        ''' <summary>
        ''' Get all my sessions
        ''' </summary>
        ''' <returns>Array of ActiveSiession</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function GetMySessions() As IEnumerable(Of ActiveSessionVO)

            Try
                Dim oService As New Services.Security(Request)
                Dim oEntity As IEnumerable(Of ActiveSessionVO) = oService.GetMySessions()
                Return oEntity
            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try
        End Function

        ''' <summary>
        ''' Purge sessions, kills sessions that have expired
        ''' </summary>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Sub PurgeSessions()
            Try
                Dim oService As New Services.Security(Request)
                oService.PurgeSessions()

            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try

        End Sub

        ''' <summary>
        ''' Get all process
        ''' </summary>
        ''' <returns>Array of Process</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function GetAllProcess() As IEnumerable(Of ProcessVO)

            Try
                Dim oService As New Services.Security(Request)
                Dim oEntity As IEnumerable(Of ProcessVO) = oService.GetAllProcess()
                Return oEntity
            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try
        End Function

    End Class

End Namespace